﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Collections;
using OSGeo.MapGuide;
using System.Xml;
using System.IO;

namespace NegevGas.Extensions.markup
{
    public partial class markupmain : utilityfunctions
    {
       public  String errorMsg = null;
       public  String errorDetail = null;
       public NameValueCollection args = null;
        public MarkupManager markupManager = null;
      public   Hashtable availableMarkup = null;
      public Hashtable openMarkup = null;
       public  bool refreshMap = false;
        String layoutId = null;
        public string  _sesseion;
        public string _mapName;
       
        public MarkupCommand _command;
        protected void Page_Load(object sender, EventArgs e)
        {
            //utilityfunctions.InitializeWebTiers();
            InitializeWebTier();
            _sesseion = GetRequestParameters()["SESSION"]; 
            //GetParameter(args, "SESSION");
         //   mgSessionId = GetRequestParameters()["SESSION"];
            _mapName = GetRequestParameters()["MAPNAME"];
            args = GetRequestParameters();
            /*Response.Write("\n args" + args);
            Response.Write("_mapName" + _mapName);
            Response.Write("_sesseion" + _sesseion);*/
            
                markupManager = new MarkupManager(args);
           
            

          
        
            try
            {
               
                
                UnSuccessText.Visible = false;
                String markupCommand = GetParameter(args, "MARKUPCOMMAND");
               
                if (markupCommand != null && markupCommand.Length > 0)
                {
                    int cmd = Int32.Parse(markupCommand);
                    switch (cmd)
                    {
                        case MarkupCommand.None:
                            break;
                        case MarkupCommand.Create:
                            markupManager.CreateMarkup(_sesseion, _mapName);
                            refreshMap = true;
                            break;
                        case MarkupCommand.Open:
                            markupManager.OpenMarkup();
                            refreshMap = true;
                            break;
                        case MarkupCommand.Delete:
                            if (!markupManager.DeleteMarkup())
                            {
                                UnSuccessText.Visible = true;
                                UnSuccessText.Text = "<div style='background-color:#F5D0D9; border:solid 1px #000000; width:200px; text-align:center;'>÷ééîåú øùåîåú úçæå÷ä, ÷ùåøåú ìùëáä äæå.<br/>ìà ðéúï ìîçå÷ àú äùëáä.</div>";
                            }
                            break;
                        case MarkupCommand.Refresh:
                            break;
                        case MarkupCommand.Close:
                            markupManager.CloseMarkup();
                            refreshMap = true;
                            break;
                    }
                }
                availableMarkup = markupManager.GetAvailableMarkup();
               
                /*availableMarkup = markupManager.GetAvailableMarkup();*/
                openMarkup = markupManager.GetOpenMarkup();

                // Remove open markup from the list of available markup.
                foreach (String key in openMarkup.Keys)
                {
                    if (availableMarkup.ContainsKey(key))
                    {
                        availableMarkup.Remove(key);
                    }
                }
            }
            catch (MgException mge)
            {
                errorMsg += mge.GetExceptionMessage();
               // errorMsg = mge.InnerException.Message + " ///" + mge.InnerException.Source;
                errorDetail = mge.GetDetails();
            }
            catch (Exception eee)
            {
               errorMsg += eee.Message;
            }

        }
    }
}